package com.bread.tliaswebmanagement.controller;

import com.bread.tliaswebmanagement.anno.Log;
import com.bread.tliaswebmanagement.pojo.PageResult;
import com.bread.tliaswebmanagement.pojo.Result;
import com.bread.tliaswebmanagement.pojo.Student;
import com.bread.tliaswebmanagement.pojo.StudentQueryParam;
import com.bread.tliaswebmanagement.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/students")
public class StudentController {

    @Autowired
    private StudentService studentService;
    @GetMapping
    public Result page(StudentQueryParam studentQueryParam){
        log.info("分页查询:{}", studentQueryParam);
        PageResult<Student> page = studentService.page(studentQueryParam);
        return Result.success(page);
    }
    @Log
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("批量删除学生:{}", ids);
        studentService.deleteByIds(ids);
        return Result.success();
    }

    @Log
    @PostMapping
    public Result add(@RequestBody Student student){
        log.info("新增学生:{}", student);
        studentService.add(student);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("根据id查询学生:{}", id);
        Student student = studentService.getById(id);
        return Result.success(student);
    }

    @Log
    @PutMapping
    public Result update(@RequestBody Student student){
        log.info("修改学生:{}", student);
        studentService.update(student);
        return Result.success();
    }

    @Log
    @PutMapping("/violation/{id}/{score}")
    public Result updateViolation(@PathVariable Integer id, @PathVariable Integer score){
        log.info("修改学生违纪分数:{},{}", id, score);
        studentService.updateViolation(id, score);
        return Result.success();
    }

}
